class Main extends eui.UILayer {

    /**
     * 构造函数解析URL
     */
    public constructor() {
        super();

        GameConfig.appendConfig(document["config"]);
        GameConfig.init();
    }

    protected createChildren(): void {
        super.createChildren();

        acelab.init(this.stage);

        this.stage.dirtyRegionPolicy = egret.DirtyRegionPolicy.OFF;

        this.stage.registerImplementation("eui.IAssetAdapter", new AssetAdapter());
        this.stage.registerImplementation("eui.IThemeAdapter", new ThemeAdapter());

        RES.addEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.onConfigComplete, this);
        RES.loadConfig("resource/default.res.json", "resource/");
    }

    private onConfigComplete(): void {
        RES.removeEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.onConfigComplete, this);

        let theme = new eui.Theme("resource/default.thm.json", this.stage);
        theme.addEventListener(eui.UIEvent.COMPLETE, this.onThemeLoadComplete, this);
    }

    private onThemeLoadComplete(): void {
        RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);

        this.loadConfigResource();
    }

    /**
     * 资源加载完成
     * @param e
     */
    private onResourceLoadComplete(e: RES.ResourceEvent): void {
        if (e.groupName == "loading") {
            RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);

            this.startGame();
        }
    }

    private startGame() {
        game.init();

        gameManager.open("LoadingScene");

        if (!StringUtils.getUrlParams("code")) {
            let logo: LogoScene = new LogoScene();
            logo.show();
        }

        console.log("*************************************************************");
        console.log("*************************************************************");
    }

    //资源配置
    private loadConfigResource() {

        let total: number = GameConfig.res_common_list.length + GameConfig.res_game_list.length;
        let index: number = 0;

        if (total == 0) {
            RES.loadGroup("loading");
            return;
        }

        function loadit(key: string, url: string) {
            RES.getResByUrl(url, function (res) {
                ++index;

                if (res) {
                    GameConfig.res_config[key] = res;
                }
                if (index == GameConfig.res_common_list.length + GameConfig.res_game_list.length) {
                    RES.loadGroup("loading");
                }
            }, this, RES.ResourceItem.TYPE_IMAGE);
        }

        if (GameConfig.res_common_list) {
           for (let i: number = 0; i < GameConfig.res_common_list.length; i++) {
               loadit(GameConfig.res_common_list[i].split('.')[0], GameConfig.client_season + GameConfig.res_common_list[i] + '?v=' + GameConfig.version_res);
           }
        }
        if (GameConfig.res_game_list) {
           for (let i: number = 0; i < GameConfig.res_game_list.length; i++) {
               loadit(GameConfig.res_game_list[i].split('.')[0], GameConfig.client_area + GameConfig.res_game_list[i] + '?v=' +  GameConfig.version_res);
           }
        }

        // if (GameConfig.res_common_list) {
        //     for (let i: number = 0; i < GameConfig.res_common_list.length; i++) {
        //         GameConfig.res_config[GameConfig.res_common_list[i].split('.')[0]] = null;
        //     }
        // }
        // if (GameConfig.res_game_list) {
        //     for (let i: number = 0; i < GameConfig.res_game_list.length; i++) {
        //         GameConfig.res_config[GameConfig.res_game_list[i].split('.')[0]] = null;
        //     }
        // }
        //
        // RES.loadGroup("loading");
    }
}